package flc.ast.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.PictureClsAdapter;
import flc.ast.bean.PictureClsBean;
import flc.ast.dataBase.AlbumRepository;
import flc.ast.databinding.ActivityAlbumManageBinding;
import flc.ast.dialog.ClassificationDialog;
import flc.ast.dialog.SelDateDialog;
import stark.common.basic.event.EventStatProxy;

public class AlbumManageActivity extends BaseAc<ActivityAlbumManageBinding> {

    private PictureClsAdapter pictureClsAdapter;
    private List<PictureClsBean> listShow;
    private int type;
    private long time;
    private String location, label;
    private SelDateDialog selDateDialog;

    @Override
    protected int onCreate() {
        return R.layout.activity_album_manage;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(this, mDataBinding.event5Container);

        mDataBinding.ivAlbumManageBack.setOnClickListener(v -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.ivSelDate.setOnClickListener(this);
        mDataBinding.ivSearch.setOnClickListener(this);
        mDataBinding.ivAlbumManage.setOnClickListener(this);
        mDataBinding.tvClassification.setOnClickListener(this);

        mDataBinding.rvPictureList.setLayoutManager(new LinearLayoutManager(mContext));
        pictureClsAdapter = new PictureClsAdapter();
        mDataBinding.rvPictureList.setAdapter(pictureClsAdapter);
        pictureClsAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.ivSelDate) {//选择日期
            showSelDateDialog();
        } else if (id == R.id.ivAlbumManage || id == R.id.tvClassification) {//相册管理
            showClassificationDialog();
        } else if (id == R.id.ivSearch) {//搜索
            SearchActivity.type = type;
            startActivity(SearchActivity.class);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        if (type == 0) {
            if (time == 0) {
                listShow = new AlbumRepository(mContext).getImagesGroupedByDate();
            } else {
                listShow = new AlbumRepository(mContext).getImagesGroupedByDateRange(time);
            }
        } else if (type == 1) {
            if (TextUtils.isEmpty(location)) {
                listShow = new AlbumRepository(mContext).getImagesGroupedByCity();
            } else {
                listShow = new AlbumRepository(mContext).getImagesByCity(location);
            }
        } else if (type == 2) {
            if (TextUtils.isEmpty(label)) {
                listShow = new AlbumRepository(mContext).getImagesGroupedByTag();
            } else {
                listShow = new AlbumRepository(mContext).getImagesByTag(label);
            }
        }
        if (listShow != null && listShow.size() != 0) {
            pictureClsAdapter.setList(listShow);
            mDataBinding.rvPictureList.setVisibility(View.VISIBLE);
            mDataBinding.tvPictureNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvPictureList.setVisibility(View.GONE);
            mDataBinding.tvPictureNoData.setVisibility(View.VISIBLE);
        }
    }

    private void showClassificationDialog() {
        ClassificationDialog dialog = new ClassificationDialog(mContext);
        dialog.setSelPosition(type);
        dialog.setListener(new ClassificationDialog.IListener() {
            @Override
            public void clickItemName(int position, String name) {
                mDataBinding.tvClassification.setText(name);
                type = position;
                getData();
                if (position == 0) {
                    mDataBinding.ivSelDate.setVisibility(View.VISIBLE);
                    mDataBinding.ivSearch.setVisibility(View.GONE);
                } else {
                    mDataBinding.ivSelDate.setVisibility(View.GONE);
                    mDataBinding.ivSearch.setVisibility(View.VISIBLE);
                }
            }
        });
        dialog.show();
    }

    private void showSelDateDialog() {
        if (selDateDialog == null) {
            selDateDialog = new SelDateDialog(mContext);
        }
        selDateDialog.setListener(new SelDateDialog.Ilistener() {
            @Override
            public void clickConfirm(long date) {
                time = date;
                getData();
            }
        });
        selDateDialog.show();
    }


}